using Godot;

[Tool]
public partial class ChromaWall : StaticBody2D
{
    [ExportGroup("Settings")]
    [Export]
    public Vector2 Size
    {
        get => _size;
        set { _size = value; UpdateWall(); }
    }
    private Vector2 _size = new Vector2(64, 64);

    [Export]
    public BlockColor ColorType
    {
        get => _colorType;
        set { _colorType = value; UpdateWall(); }
    }
    private BlockColor _colorType = BlockColor.Red;

    [ExportGroup("Internal Refs")]
    [Export] public Sprite2D VisualSprite;
    [Export] public CollisionShape2D Collider;




    // Layer 常量
    private const int LAYER_WORLD = 1;
    private const int LAYER_RED = 2;
    private const int LAYER_BLUE = 3;

    private const int LAYER_GREEN = 4;

    public override void _Ready()
    {
        UpdateWall();
    }

    private void UpdateWall()
    {
        // 1. 更新大小 (纹理平铺)
        if (VisualSprite != null)
        {
            VisualSprite.RegionRect = new Rect2(Vector2.Zero, _size);
        }
        if (Collider != null && Collider.Shape is RectangleShape2D rect)
        {
            rect.Size = _size;
        }

        // 2. 更新物理层 (Layer - 我是谁)
        // StaticBody 不需要 Mask (因为它不主动撞人)，只要设置 Layer 让人撞即可
        SetCollisionLayerValue(LAYER_WORLD, false);
        SetCollisionLayerValue(LAYER_RED, false);
        SetCollisionLayerValue(LAYER_BLUE, false);
        SetCollisionLayerValue(LAYER_GREEN, false);

        switch (_colorType)
        {
            case BlockColor.Red:
                if (VisualSprite != null) VisualSprite.Modulate = Colors.Red;
                SetCollisionLayerValue(LAYER_RED, true);
                break;
            case BlockColor.Blue:
                if (VisualSprite != null) VisualSprite.Modulate = Colors.Cyan;
                SetCollisionLayerValue(LAYER_BLUE, true);
                break;
            case BlockColor.Green:
                if (VisualSprite != null) VisualSprite.Modulate = Colors.Lime;
                SetCollisionLayerValue(LAYER_GREEN, true);
                break;
            case BlockColor.World:
                if (VisualSprite != null) VisualSprite.Modulate = Colors.White;
                SetCollisionLayerValue(LAYER_WORLD, true);
                break;
        }

        UpdateSound();
    }

    [ExportGroup("Audio")]
    [Export] public AudioStreamPlayer2D HumSound;
    private void UpdateSound()
    {

        if (HumSound == null) return;
        if (Engine.IsEditorHint() || !IsInsideTree())
        {
            return;
        }

        float targetPitch = 1.0f;

        switch (ColorType)
        {
            case BlockColor.Red:
                targetPitch = Cfg.RedPitch;
                break;
            case BlockColor.Blue:
                targetPitch = Cfg.BluePitch;
                break;
            case BlockColor.Green:
                targetPitch = Cfg.GreenPitch;
                break;
            case BlockColor.World:
                targetPitch = 1.0f;
                break;
        }
        HumSound.PitchScale = targetPitch;
        // 2. 确保它在播放
        if (!HumSound.Playing && !Engine.IsEditorHint())
        {
            HumSound.Play();
        }
    }
}

